package com.example.Activity;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.NotificationCompat;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class RegisterActivity extends Activity{
    EditText username,password;
    Button register;
    static final String URL ="http://120.24.37.161:8080/17376058_CZP_FinalTest/register";
    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler(){
        public void handleMessage(Message msg){
            switch (msg.what){
                case 0:
                    Toast.makeText(RegisterActivity.this, "注册失败", Toast.LENGTH_LONG).show();
                    break;
                case 1:
                    Intent intent = new Intent(RegisterActivity.this,LoginActivity.class);
                    PendingIntent pendingIntent = PendingIntent.getActivity(RegisterActivity.this,0,intent,0);
                    NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
                    Notification notification = new NotificationCompat.Builder(RegisterActivity.this, "first")
                            .setContentTitle("注册成功")
                            .setContentText("用户 " + username.getText().toString() + "  注册成功")
                            .setWhen(System.currentTimeMillis())
                            .setSmallIcon(R.drawable.ic_location)
                            .setAutoCancel(true)
                            .setContentIntent(pendingIntent)
                            .setDefaults(NotificationCompat.DEFAULT_ALL)
                            .setPriority(NotificationCompat.PRIORITY_MAX)
                            .build();
                    manager.notify(1, notification);
//                    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
//                    Notification notification = new NotificationCompat.Builder(RegisterActivity.this)
//                            .setContentTitle("注册成功")
//                            .setContentText("用户 " + username.getText().toString() + "  注册成功")
//                            .setWhen(System.currentTimeMillis())
//                            .setSmallIcon(R.drawable.ic_launcher_background)
//                            .setAutoCancel(true)
//                            .build();
//                    manager.notify(1, notification);
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_LONG).show();
                    break;
                case 2:
                    Toast.makeText(RegisterActivity.this, "当前无网络,不知道的问题", Toast.LENGTH_LONG).show();
                    break;
                    default:
                        break;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_activity);
        initView();
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                sendRequestWithOkHttp();

//                finish();
            }
        });
    }

    private void initView() {
        username = findViewById(R.id.r_username);
        password = findViewById(R.id.r_password);
        register = findViewById(R.id.btn_register);

    }


    private void sendRequestWithOkHttp() {
        if((username.getText().toString() != null&&username.getText().toString().length() != 0)
                &&(password.getText().toString() != null&&password.getText().toString().length() != 0)){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        OkHttpClient client = new OkHttpClient();
                        RequestBody requestBody = new FormBody.Builder()
                                .add("username",username.getText().toString())
                                .add("password",password.getText().toString())
                                .build();
                        Request request = new Request.Builder()
                                .url(URL)
                                .post(requestBody)
                                .build();
                        Response response = client.newCall(request).execute();
                        if (response.body().string().equals("1")) {
                            Message message = new Message();
                            message.what = 1;
                            handler.sendMessage(message);
                        } else {
                            Message message = new Message();
                            message.what = 0;
                            handler.sendMessage(message);
                        }
                        Log.d("RegisterActivity","没有错误"+username.getText().toString());
//                        String responseData = response.body().string();
//                        Log.d("register",responseData);
                    } catch (IOException e) {
                        e.printStackTrace();
                        Message message = new Message();
                        message.what = 2;
                        handler.sendMessage(message);
                    }
                }
            }).start();
        }
        else {
            Toast.makeText(RegisterActivity.this, "不符合要求,用户或密码不能为空", Toast.LENGTH_LONG).show();
        }
    }


}
